package com.wsoft.hr.pm.manager;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.HrPmRedisKeyConstant;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.BaseEntity;
import com.wsoft.hr.pm.entity.HrPmScopeEntity;
import com.wsoft.hr.pm.mapper.HrPmScopeMapper;
import com.wsoft.hr.pm.query.scope.ScopeListQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2025年02月17日 15:06:35
 */
@Slf4j
@Service
public class HrPmScopeManager extends ServiceImpl<HrPmScopeMapper, HrPmScopeEntity> {


    @Resource
    private RedisService redisService;


    @GetRedisCache(type = HrPmRedisKeyConstant.SCOPE_INFO,
            expire_sec = HrPmRedisKeyConstant.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public HrPmScopeEntity getById(Long id){
        return super.getById(id);
    }


    /**
     * 修改维度
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(HrPmScopeEntity entity){
        boolean result = super.updateById(entity);
        if (result){
            redisService.del(HrPmRedisKeyConstant.SCOPE_INFO + entity.getId());
        }
        return result;
    }

    /**
     * 删除维度
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        boolean result = super.removeById(id);
        if (result){
            redisService.del(HrPmRedisKeyConstant.SCOPE_INFO + id);
        }
        return result;
    }

    /**
     * 根据组织和名称查询
     * @param hrOrgId
     * @param name
     * @return
     */
    public HrPmScopeEntity getOrgAndName(Long hrOrgId, String name){
        QueryWrapper<HrPmScopeEntity> query = new QueryWrapper<>();
        query.lambda().eq(HrPmScopeEntity::getHrOrgId,hrOrgId)
                .eq(HrPmScopeEntity::getName,name);
        return getOne(query);
    }


    /**
     * 列表查询
     * @param param
     * @return
     */
    public List<HrPmScopeEntity> list(ScopeListQuery param) {
        QueryWrapper<HrPmScopeEntity> query = new QueryWrapper<>();
        query.lambda().like(StrUtil.isNotBlank(param.getName()), HrPmScopeEntity::getName,param.getName())
                .orderByDesc(BaseEntity::getCreateTime);
        return super.list(query);
    }
}
